<?php

namespace services\statistics;

use Yii;
use common\models\base\Project;
use common\models\patient\User;
use common\models\clinic\Clinic;
use common\components\Service;

/**
 * Class PerfectionReportService
 * @package services\statistics
 */
class PerfectionReportService extends Service
{
    /**
     * 科室病人月就诊次数人数汇总表
     *
     * @param $projectId
     * @param $monthIs
     * @return array
     */
    public function getPerfection($projectId, $monthIs)
    {
        // 诊室列表
        $clinic = Clinic::find()
            ->select('id, name, sort')
            ->where([
                'project_id' => $projectId,
                'status' => Clinic::STATUS_ENABLE
            ])
            ->orderBy(['sort' => SORT_ASC, 'created_at' => SORT_DESC])
            ->asArray()
            ->all();

        $query = User::find()
            ->select('id, degree_num, clinic_id')
            ->where(['project_id' => $projectId]);
        if ($monthIs) {
            // 月末时间戳
            $monthNext =  strtotime(date('Y-m-d', $monthIs) . "+1 month") - 1;
            $query->andWhere(['between', 'last_revisiting_time', $monthIs, $monthNext]);
        }

        // $list = $query->createCommand()->getRawSql();
        $list = $query
            ->asArray()
            ->all();

        // 诊室整理
        $clinicList = [];
        foreach ($clinic as $cl) {
            $clinicList[$cl['id']]['name'] = $cl['name'];
            $clinicList[$cl['id']]['num'] = 0;
            $clinicList[$cl['id']]['not_perfect_num'] = 0;
        }

        // 患者数
        foreach ($list as $li) {
            if (isset($clinicList[$li['clinic_id']])) {
                // 患者数
                $clinicList[$li['clinic_id']]['num'] = $clinicList[$li['clinic_id']]['num'] + 1;

                // 未完善患者数
                if ($li['degree_num'] < 1) {
                    $clinicList[$li['clinic_id']]['not_perfect_num'] = $clinicList[$li['clinic_id']]['not_perfect_num'] + 1;
                }
            }
        }

        $clinicList = array_values($clinicList);
        // 完善率
        foreach ($clinicList as &$val) {
            if ($val['num']) {
                $val['percent_num'] = round((($val['num'] - $val['not_perfect_num']) / $val['num'] * 100), 2) . '%';
            }
        }

        return $clinicList;
    }

    /**
     * 项目病人月就诊次数人数汇总表
     *
     * @param $monthIs
     * @return array
     */
    public function getPerfectionAll($monthIs)
    {
        // 项目列表
        $clinic = Project::find()
            ->select('id, name, sort')
            ->where([
                'status' => Project::STATUS_ENABLE
            ])
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_ASC])
            ->asArray()
            ->all();

        $query = User::find()
            ->select('id, degree_num, project_id');
        if ($monthIs) {
            // 月末时间戳
            $monthNext =  strtotime(date('Y-m-d', $monthIs) . "+1 month") - 1;
            $query->andWhere(['between', 'last_revisiting_time', $monthIs, $monthNext]);
        }

        $list = $query
            ->asArray()
            ->all();

        // 项目整理
        $projectList = [];
        foreach ($clinic as $cl) {
            $projectList[$cl['id']]['name'] = $cl['name'];
            $projectList[$cl['id']]['num'] = 0;
            $projectList[$cl['id']]['not_perfect_num'] = 0;
        }

        // 患者数
        foreach ($list as $li) {
            if (isset($projectList[$li['project_id']])) {
                // 患者数
                $projectList[$li['project_id']]['num'] = $projectList[$li['project_id']]['num'] + 1;

                // 未完善患者数
                if ($li['degree_num'] < 1) {
                    $projectList[$li['project_id']]['not_perfect_num'] = $projectList[$li['project_id']]['not_perfect_num'] + 1;
                }
            }
        }

        $projectList = array_values($projectList);
        // 完善率
        foreach ($projectList as &$val) {
            if ($val['num']) {
                $val['percent_num'] = round((($val['num'] - $val['not_perfect_num']) / $val['num'] * 100), 2) . '%';
            }
        }

        return $projectList;
    }

}
